package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.AdvertInfopic;
import com.zl.smartmall.library.po.ShopsIntroduce;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 考拉合作加盟商家
 *
 */
public abstract class ShopsIntroduceListener extends BaseListener {

	private static final String TAG = ShopsIntroduceListener.class
			.getSimpleName();

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

		try {
			int status = response.getInt(Constants.REQ_STATUS);
			String msg = response.getString(Constants.REQ_MSG);
			CrashHandler.d(TAG, "商城活动(首)页服务器status = " + status);
			CrashHandler.d(TAG, "商城活动(首)页服务器msg = " + msg);

			switch (status) {
			case Constants.REQ_OK:
				JSONObject jsonObject = response
						.getJSONObject(Constants.REQ_RESULT);
				JSONArray listArray = jsonObject.getJSONArray("list1");
				JSONArray listArray2 = jsonObject.getJSONArray("list2");
				JSONArray listArray3 = jsonObject.getJSONArray("list3");
				List<ShopsIntroduce> shopinfolist1 = new ArrayList<ShopsIntroduce>();
				List<ShopsIntroduce> shopinfolist2 = new ArrayList<ShopsIntroduce>();
				List<ShopsIntroduce> shopinfolist3 = new ArrayList<ShopsIntroduce>();
				for (int i = 0; i < listArray.length(); i++) {
					JSONObject advertise = listArray.getJSONObject(i);
					ShopsIntroduce advert = new ShopsIntroduce();
					if (advertise.getInt("shop_type") == 1) {
						advert.setPic(jsonObject.getString("images_host") + advertise.getString("pic"));
						advert.setName(advertise.getString("name"));
						advert.setShoptype(advertise.getInt("shop_type"));
					}
					shopinfolist1.add(advert);
				}
				for (int j = 0; j < listArray2.length(); j++) {
					JSONObject advertise = listArray2.getJSONObject(j);
					ShopsIntroduce advert = new ShopsIntroduce();
					advert.setPic(jsonObject.getString("images_host") + advertise.getString("pic"));
					if (advertise.getInt("shop_type") == 2) {
						advert.setName(advertise.getString("name"));
						advert.setShoptype(advertise.getInt("shop_type"));
					}
					shopinfolist2.add(advert);
				}
				for (int k = 0; k < listArray3.length(); k++) {
					JSONObject advertise = listArray3.getJSONObject(k);
					ShopsIntroduce advert = new ShopsIntroduce();
					advert.setPic(jsonObject.getString("images_host") + advertise.getString("pic"));
					if (advertise.getInt("shop_type") == 3) {
						advert.setName(advertise.getString("name"));
						advert.setShoptype(advertise.getInt("shop_type"));
					}
					shopinfolist3.add(advert);
				}
				onResponseSuccess(shopinfolist1, shopinfolist2, shopinfolist3);
				break;

			case Constants.REQ_OFFLINE:
				onOffline();
				break;

			case Constants.REQ_NO_MORE_DATA:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
			}

		} catch (JSONException e) {
			onFailure(statusCode, headers, e, new JSONObject());
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}

	/**
	 * 闪屏页广告加载成功
	 */
	public abstract void onResponseSuccess(List<ShopsIntroduce> shopinfo1,
			List<ShopsIntroduce> shopinfo2,List<ShopsIntroduce> shopinfo3);

}
